import { reqGoodsInfo, reqAddOrUpdateShortCart } from "@/api"
//封装游客身份模块uuid--->生成一个随机字符串（不能在变了）
import { getUUID } from '@/utils/uuid_token'
//定义仓库的数据
const state = {
    goodInfo: {},
    //游客临时身份
    uuid_token: getUUID()
}
// 唯一修改 state 的地方
const mutations = {
    GETGOODINFO(state, goodInfo) {
        state.goodInfo = goodInfo
    }
}
// 处理派发action，处理异步语句，编辑逻辑
const actions = {
    // 获取数据的
    async getGoodInfo({ commit }, skuId){
        let result = await reqGoodsInfo(skuId)
        if(result.code == 200) {
            commit("GETGOODINFO",result.data)
        }
    },
    async addOrUpdateShortCart({ skuId, skuNum }) {
        let result = await reqAddOrUpdateShortCart(skuId,skuNum)
        if(result == 200) {
            return "OK"
        }else {
            return Promise.reject(new Error('false'))
        }
    }
}
// 计算属性
const getters = {
    // 商品导航
    categoryView(state) {
        return state.goodInfo.categoryView || {}
    },
    // 商品详情
    skuInfo(state) {
        return state.goodInfo.skuInfo || {}
    },
    // 商品售卖属性
    spuSaleAttrList(state) {
        return state.goodInfo.spuSaleAttrList || {}
    }
}

export default {
    state,
    mutations,
    actions,
    getters
}